import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../service/http.service';
import { HttpRouterService } from '../../service/http-router.service';
import { StorageService } from '../../service/storage.service';
import { Router } from '@angular/router';
import _ from 'underscore';

@Component({
  selector: 'app-order',
  templateUrl: './order.component.html',
  styleUrls: ['./order.component.less']
})
export class OrderComponent implements OnInit {

  public step = '';
  public empty = false;

  public orderList = [];
  public followtime = "";
  public followrecord = "";
  public page = 1;
  public showAdd = true;


  constructor(
    private _http: HttpService,
    private _router: Router,
    private _storage: StorageService,
    private _httpRouter: HttpRouterService
  ) { }

  ngOnInit() {
    if (this._storage.getSession('token')) {
      this.showAdd = true;
    }else {
      this.showAdd = false;
    }
    this.getOrderList();
  }

  // 展示订单
  getOrderList() {
    let param = "followtime="+this.followtime
      +"&followrecord="+this.followrecord
      +'&page=' + this.page
      + '&token=' + this._storage.getSession('token')


    this._http.post(this._httpRouter.orderList, param).then(res => {
      if (res.code === 1) {
        if (res.data.length < 20) {
          this.showAdd = false;
        }
        if (res.data.length === 0) {
          this.empty = true;
        }
        this.orderList = _.union(this.orderList, res.data);
        console.log(this.orderList);
      }
    });
  }
  serch(){
    this.page = 1;
    this.getOrderList();
  }

  more($event) {
    $event.preventDefault();
    this.page += 1;
    this.getOrderList();
  }

  // 跳转详情
  orderDetail(customerId,oId) {
    this._storage.setSession('danId', customerId);
    this._storage.setSession('oId', oId);
    this._router.navigate(['/attache/order-details']);
  }

}
